package main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.ByteArrayInputStream;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.plaf.PanelUI;

import parser.TartaParser;
import exec.Executor;
import graphic.Turtle;

public class TartaFrame extends JFrame implements ActionListener{
	
	//Graphics2D graphicState = null;
	
	//definição de Paineis para criação da interface
	private JPanel mainPanel =  new JPanel(); 	
	private MyJPanel mainTurtlePanel = new MyJPanel();
	private JPanel sul;

	// definição de componentes a serem utilizados nos paineis
	private JTextArea ja = new JTextArea(35,25);
	private JButton jb1 = new JButton("Executa");
	private JButton jb2 = new JButton("Limpa");
	private JTextField jf1 = new JTextField(30), jf2 = new JTextField(30);
	
	//private PanelUI update;
	private Vector allCommands = new Vector();
	
	
	private void setCommands(Vector commandsVector)
	{
		allCommands = commandsVector;		
	}

	
	//constrututor público
	public TartaFrame (String name)
	{
		super(name);
	}
	
	// inicialização de componentes
	public void initComponents()
	{
		//this.setMinimumSize(new Dimension(600,600));
		
		// definição de Painel Principal
		mainPanel.setLayout(new BorderLayout(5,1));
		
		// painel inferior com FlowLayout
		sul =  new JPanel(new FlowLayout());
		sul.add(new JLabel("Demora: "));
		sul.add(jf1);
		sul.add(jb1);
		jf2.setEditable(false);
		sul.add(jf2);
		sul.add(jb2);
		
		//ajuste do painel para desenhos
		mainTurtlePanel.setBackground(Color.YELLOW);
		mainTurtlePanel.setSize(800, 800);
		
		
		//adição de paineis ao painel principal
		mainPanel.add("South", sul);
		mainPanel.add("East", ja);
		//mainTurtlePanel.setIgnoreRepaint(true);
		mainPanel.add("Center", mainTurtlePanel);
		
		//adiciona listener ao Botao de Execucao
		jb1.addActionListener(this);
		
		//adiciona listener ao Botao de Limpeza 
		jb2.addActionListener(this);		
		
		//adiciona listener à janela principal
		//this.addWindowListener(this);
		
		//adiciona o Panel Principal ao Frame
		this.add(mainPanel);
		
		Toolkit toolkit = Toolkit.getDefaultToolkit();
		Dimension dimension = new Dimension((int)toolkit.getScreenSize().getWidth()-200,(int)toolkit.getScreenSize().getHeight()-200);
		this.setMinimumSize(dimension);
	}
	
	
	
	Turtle tutT = null;
	public void actionPerformed(ActionEvent e) {
		if ( e.getSource() == jb1 ) // executa
		{
			String s = ja.getText();
			ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
			Executor ex = new TartaParser(is).parse();
			if ( ex == null )
			{
				jf2.setText("Programa invalido.");
				return;
			}
			else
				jf2.setText("");
			
			if ( tutT == null )
				tutT = new Turtle(this, mainTurtlePanel, 800, 800); 
			
			ex.setTurtle(tutT);
			ex.executa();
			
			mainTurtlePanel.setDrawCommands(tutT.getCommands());			
		}
		else if(e.getSource()==jb2) // limpa
		{
		
		int result = JOptionPane.showConfirmDialog(null, 
				"Deseja Limpar a Tela?", "Limpar Tela?", JOptionPane.YES_NO_OPTION);

					if(result == JOptionPane.YES_OPTION){
					
					tutT = null;
					allCommands.clear();
					mainTurtlePanel.setDrawCommands(allCommands);
					this.repaint();
					}
					
		}
		
	}
}


class MyJPanel extends JPanel
{
	private Vector drawCommands = new Vector();
	
	
	public void setDrawCommands(Vector drawCommands)
	{
		this.drawCommands = drawCommands;
	}
	
	
	public void paint(Graphics g)
	{	
		super.paint(g);
		int params[] = new int[4];
		try{
		for (int i=0; i<drawCommands.size(); i++)
		{
		params = (int[]) drawCommands.elementAt(i);
		g.drawLine(params[0], params[1], params[2],params[3]);
		}}
		catch(Exception e)
		{ 
			//System.out.println("Nenhum commando foi dado ainda "+e);
		}
	}
}

